﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fengsoft.AIBlog.Web.Code;
using Fengsoft.AIBlog.Web.Models;
using Fengsoft.FengCommerce.Resources;
namespace Fengsoft.AIBlog.Web.Controllers
{
    public class HomeController:ExtensionController
    {
        private CarouselModel CreateCarousel(string[] fileNames)
        {
            CarouselModel result=new CarouselModel();
            int itemCount=4;
            
            if (fileNames.Length<itemCount)
            {
                itemCount=fileNames.Length;
            }
            List<int> indexes=new List<int>(itemCount);
            Random random=new Random();
            while (indexes.Count<itemCount)
            {
                int index=random.Next(0,fileNames.Length-1);
                if (!indexes.Any(x=>x==index))
                {
                    indexes.Add(index);
                }
            }
            foreach (int i in indexes)
            {
                FileInfo file=new FileInfo(fileNames[i]);
                string fileName=file.Name;
                string extension=file.Extension;
                string id=fileName.Replace(extension,string.Empty);
                CarouselItemModel item=new CarouselItemModel();
                item.ImageUrl=string.Format("~/blobinfo/image/{0}",id);
                item.Caption=id.Substring(0,id.Length-4).Replace("-"," ");
                result.Items.Add(item);
            }
            return result;
        }
        public ActionResult Index(string sidx,string sord,int page=1,int rows=16)
        {
            HomeIndexModel model=new HomeIndexModel();
            string[] fileNames=CacheHelper.Blobs.Select(x => x.FileName).ToArray();
            PagerModel pager=new PagerModel();
            ThumbnailListModel thumbnailList=new ThumbnailListModel();
            model.Carousel=CreateCarousel(fileNames);
            pager.PageSize=rows;
            pager.TotalRecords=fileNames.Length;
            pager.CurrentPage=page;
            fileNames=fileNames.OrderBy(x => x).Skip(pager.Skip).Take(pager.PageSize).ToArray();
            var pagedNames=CacheHelper.IdolNames.OrderBy(x => x).ToArray();
            foreach (string name in pagedNames)
            {
                BlobModel blob=CacheHelper.Blobs.Where(x => x.Name==name).OrderByDescending(x => x.ModifiedDate).First();
                ThumbnailModel thumbnail=new ThumbnailModel();
                thumbnail.ImageViewerUrl=string.Format("~/gallery/thumbnaillist/{0}",blob.SeoName);
                thumbnail.ThumbnailUrl=string.Format("~/blobinfo/thumbnail/{0}?width=320&height=180",blob.SeoFileName);
                thumbnail.Title=blob.Name;
                thumbnailList.Thumbnails.Add(thumbnail);
            }
            model.GalleryPager=pager;
            model.Gallery=thumbnailList;
            return View(model);
        }
        public ActionResult ChangeCulture(string cultureName,string url)
        {
            Uri redirectUrl=new Uri(url);
            if (!Request.Url.Host.Equals(redirectUrl.Host,StringComparison.OrdinalIgnoreCase))
            {
                throw new ApplicationException("Invalid URL");
            }
            cultureName=CultureHelper.GetValidCulture(cultureName); // Validate input.
            // Save culture in a cookie.
            CookieHelper.CultureName=cultureName;
            if (string.IsNullOrEmpty(url))
            {
                return RedirectToAction("index");
            }
            else
            {
                return Redirect(url);
            }
        }
        public ActionResult ChangeLayout(string layout)
        {
            CookieHelper.Layout=layout;
            return Content(layout);
        }
        [HttpPost()]
        public ActionResult ChangeTheme(string theme)
        {
            CookieHelper.MvcTheme=theme;
            return Content(theme);
        }
        public ActionResult About()
        {
            ViewBag.Message="Your application description page.";

            return View();
        }
        public ActionResult Contact()
        {
            ViewBag.Message="Your contact page.";

            return View();
        }
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult Error403()
        {
            return View();
        }
        public ActionResult Error404()
        {
            return View();
        }
        [Route("admin/dashboard")]
        public ActionResult Dashboard()
        {
            if (SessionHelper.IsAdmin)
            {
                return View();
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }
    }
}